*==========================================================================================================================================================
*
*	Local Projections
*
*==========================================================================================================================================================


set more off

qui {


*-------------------------------------------------------------------------------------------------------------
*	SETTINGS
*-------------------------------------------------------------------------------------------------------------

* Variables
local vars "log_R_esp_er_hp log_R_esp_er_hp_hi log_R_esp_er_hp_lo" 
local lava ""Lending rate" "Upper bound" "Lower bound"" 
local yti ""Percentage pts" "Percentage pts" "Percentage pts""

* irf horizon
local H 5
local F 5

* lags
local L 2
local LS `L'

*-------------------------------------------------------------------------------------------------------------

*_________ GET DATA ____________
use "`path'data/working files/prepared.dta", clear 


*____ DECLARE TIME SERIES _____
gen id =1
tsset id year, yearly

	
local count = 0
foreach v of local vars {
local count = `count'+1
n di "Variable `v'"


	

*_________________ LOCAL PROJECTIONS _________________

***_________ CONTROLS

local indep	" gdp_r_adj_gr cpi_sil_gr wage_sil_gr itsN_gr" 

forvalues i=0/`L'{
	local indep_`i' = subinstr("`indep'"," "," l`i'_",.)
	}
	
local rhs ""
forvalues i = 1/`L'{
	local rhs "`rhs'`indep_`i''"
	}
	
	local not l0_`v'_gr l0_itsN_gr
	local rhs: list rhs- not
	
	


	***_________ IMPULSE RESPONSE and CONFIDENCE INTERVAL variables
	gen irf_`v' = . if _n<=`H'+1
	
	gen se_`v' = . if _n<=`H'+1

	gen ci_up_`v' = . if _n<=`H'+1
	gen ci_lo_`v' = . if _n<=`H'+1
	gen ci_up90_`v' = . if _n<=`H'+1
	gen ci_lo90_`v' = . if _n<=`H'+1
			
	gen N_`v' = . if _n<=`H'+1	

	
	
	***_________ ESTIMATE COEFFICIENTS
	
	forvalues y = 0/`H' {
		n display "projection `y'"

		newey `v'_gr`y' l(1/`LS').`v'_gr l(-`F'/`LS').perclossN_vellon l(-`F'/`LS').perclossN `rhs' l(-`F'/`LS').percloss_salvN l(-`F'/`LS').percloss_pirN l(0/`LS').temperature l(0/`LS').warbeu l(0/`LS').warbciv, lag(5) force
		
		* store IMPULSE RESPONSES and STANDARD ERRORS
		lincom perclossN 
		replace irf_`v' = r(estimate) if _n==`y'+1 /* coefficients */
		replace se_`v' = r(se) if _n==`y'+1 /* standard errors */
		

		//restore
	}

	***_________ CONFIDENCE INTERVALS
	replace ci_up_`v' = irf_`v' + 1*se_`v' if _n <= `H'+1
	replace ci_lo_`v' = irf_`v' - 1*se_`v' if _n <= `H'+1
	replace ci_up90_`v' = irf_`v' + 1.64*se_`v' if _n <= `H'+1
	replace ci_lo90_`v' = irf_`v' - 1.64*se_`v' if _n <= `H'+1


sum ci_up90_`v'
local max = round(`r(max)'*1.2,0.5)
if (`max'<0) {
	local max = 0.5
	}

sum ci_lo90_`v'
local min = round(`r(min)'*1.33,0.5)

local text = `min'*0.7
if (`"`v'"'=="log_R_esp_er_hp" | `"`v'"'=="l_esp_rate_hp") {
	local text = `min'*0.6
	}
	
} // vars



* plot prerequisites
local lsize "med"
local tsize "med"

gen time = _n-1 if(_n<=`H'+1)
drop if time > `H'
keep time irf_* ci_up_* ci_lo_* ci_up90_* ci_lo90_* se_*
order irf_* ci_up_* ci_lo_* time ci_up90_* ci_lo90_*
drop if time == . 
gen zero = 0 if time!=.


twoway  (line irf_log_R_esp_er_hp time, lcolor(black) lpattern(solid) lwidth(thin)) ///
		(line irf_log_R_esp_er_hp_hi time, lcolor(black) lpattern(longdash) lwidth(medthick)) ///
		(line irf_log_R_esp_er_hp_lo time,  lcolor(black) lpattern(shortdash) lwidth(medthick)) ///
		(line zero time, lcolor(black) lwidth(medium)), title("") xtitle("Year", size(`lsize')) ytitle(`ytitle', height(2) size(`lsize')) xlabel(,labsize(`lsize')) ylabel(, format(%9.1f) nogrid angle(0) labsize(`lsize')) ///
		legend(order(1 2 3) label(1 "Baseline") label(2 "High") label(3 "Low") col(3) region(lcolor(white))) ///
		graphregion(color(white)) plotregion(color(white)) name(LP_rate_friction, replace) nodraw
		graph display LP_rate_friction, ysize(2.1) xsize(2.5) scale(1)
		graph export "results/FigureA10.pdf", replace 

}


*_________________________________________

			
